#include "launcher.h"

static QString get_app_path(const char *program)
{
  QString pgmName = QString(program);
#if defined(Q_OS_LINUX)
  return QCoreApplication::applicationDirPath() + 
    QDir::separator() + pgmName;
#elif defined(Q_OS_WIN32)
  return QCoreApplication::applicationDirPath() + 
    QDir::separator() + pgmName + 
    QString(".exe");
#elif defined(Q_OS_MAC)
  QDir appd = QCoreApplication::applicationDirPath();
  appd.cdUp();
  appd.cdUp();
  appd.cdUp();
if(QString(program) == QString("drishti"))
{
  appd.cd("Drishti Slabs.app");
  pgmName = QString("slabs");
}
else if(pgmName == QString("drishtipaint"))
{
  appd.cd("Drishti Paint.app");
}
else if(pgmName == QString("drishtiimport"))
{
  appd.cd("Drishti Import.app");
}

  appd.cd("Contents");
  appd.cd("MacOS");

  return appd.absoluteFilePath(pgmName);
#else
  #error Unsupported platform
#endif
}

DrishtiLauncher::DrishtiLauncher(QWidget *parent) :
  QMainWindow(parent)
{
  ui.setupUi(this);
}

void
DrishtiLauncher::on_renderer_pressed()
{
  QString app = get_app_path("drishti");

  QStringList args;
  args.clear();
  if (ui.stereo->isChecked())
    args << "-stereo";

  QProcess *process = new QProcess();
  process->start(app, args);

  if (!process->waitForStarted())
    {
      QMessageBox::critical(this,  "Drishti Launch Control",
			    QString("Unable to launch %1.").arg(app));
      return;
    }
}

void
DrishtiLauncher::on_import_pressed()
{
  QString app = get_app_path("drishtiimport");

  QStringList args;
  args.clear();

  QProcess *process = new QProcess();

//--------------------------------------
// Add path for ImageMagick libraries
#if defined(Q_WS_WIN)
  QProcessEnvironment env = QProcessEnvironment::systemEnvironment();

  env.remove("MAGICK_HOME");
  env.remove("MAGICK_CODER_MODULE_PATH");
  env.remove("MAGICK_FILTER_MODULE_PATH");

  QString imageMagickPath = QCoreApplication::applicationDirPath() +
                            "/ImageMagick";

  env.insert("PATH", imageMagickPath + ";" +
	              env.value("PATH"));

  env.insert("MAGICK_CODER_MODULE_PATH",
	     imageMagickPath + "/coders");

  env.insert("MAGICK_FILTER_MODULE_PATH",
	     imageMagickPath + "/filters");
  
  process->setProcessEnvironment(env);
#endif
//--------------------------------------

  process->start(app, args);

  if (!process->waitForStarted())
    {
      QMessageBox::critical(this,  "Drishti Launch Control",
			    QString("Unable to launch %1.").arg(app));
      return;
    }
}


void
DrishtiLauncher::on_paint_pressed()
{
  QString app = get_app_path("drishtipaint");

  QStringList args;
  args.clear();

  QProcess *process = new QProcess();
  process->start(app, args);

  if (!process->waitForStarted())
    {
      QMessageBox::critical(this,  "Drishti Launch Control",
			    QString("Unable to launch %1.").arg(app));
      return;
    }
}


void
DrishtiLauncher::on_actionExit_triggered()
{
  close();
}
